package com.jic.market.impl.activity;

import java.lang.Long;
import java.util.List;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.market.entity.activity.ActivityPriority;
import com.jic.market.mapper.activity.ActivityPriorityMapper;
import com.jic.market.request.ActivityPriorityRequest;
import com.jic.market.service.activity.ActivityPriorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 活动优先级
 * @author : ZXL
 * @email : 
 * @since : 2020-03-15 04:32:42
 * @version : v1.0.0
 */
@Service
public class ActivityPriorityServiceImpl implements ActivityPriorityService {

    @Autowired
    private ActivityPriorityMapper activityPriorityMapper;

    @Override
    public long insert(ActivityPriority activityPriority) {
        long flag = activityPriorityMapper.insert(activityPriority);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return activityPriorityMapper.delete(id);
    }

    @Override
    public long update(ActivityPriority activityPriority) {
        return activityPriorityMapper.update(activityPriority);
    }

    @Override
    public ActivityPriority queryByPrimaryKey(Long id){
        return activityPriorityMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ActivityPriority> query(ActivityPriority activityPriority){
        return activityPriorityMapper.query(activityPriority);
    }

    @Override
    public PageResult<ActivityPriority> queryPage(ActivityPriority activityPriority, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ActivityPriority> list = activityPriorityMapper.queryPage(startIndex, pageQuery.getLimit(), activityPriority);
        long count = activityPriorityMapper.queryPageCount(activityPriority);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }


    @Override
    public Boolean updateActivityPrioritySort(ActivityPriorityRequest request) {
        if (request != null && request.getSortType() != null){
            //根据排序值与类型查询出对应上面或下面的一条数据
            ActivityPriority activityPriority = activityPriorityMapper.queryPriorityInfoBySort(request.getSort(),request.getSortType());
            if (activityPriority != null) {
                activityPriorityMapper.updateSort(request.getId(),activityPriority.getSort());
                activityPriorityMapper.updateSort(activityPriority.getId(),request.getSort());
            }
            return true;
        }else {
            return false;
        }
    }
}